var prefix = jsctx + "/acctTenantOperator";

var acctTenantOperatorElecStation = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageAcctTenantOperatorElecStation",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    cache: false,
                    paginationShowPageGo: true,
                    height: h,
                    showColumns: false,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            tenantOperatorCode: $('#tenantOperatorCode').val(),
                            startTime: $('#startTime').val(),
                            endTime: $('#endTime').val(),
                            stationName: $('#stationName').val(),
                        };
                    },
                    columns: [
                        // {
                        //     checkbox: true, field: '', width: '50', align: 'left',
                        // },
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'loginName', title: '账号', width: '100', align: 'left'},
                        {field: 'operatorName', title: '分销商名称', align: 'left', width: '100'},
                        {field: 'stationName', title: '站点名称', width: '100', align: 'left'},
                        {field: 'directCurrentPile', title: '直流桩', width: '100', align: 'left'},
                        {field: 'exchangePile', title: '交流桩', width: '100', align: 'left'},
                        {field: 'pileTotalPower', title: '总功率(KW)', width: '100', align: 'left'},
                        {field: 'operateStartTime', title: '开通运营时间', width: '100', align: 'left'},
                        {
                            field: '', title: '规则有效时间', width: '180', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.ruleStartTime != null && row.ruleStartTime != '') {
                                    return row.ruleStartTime + '至' + row.ruleEndTime;
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: 'roleId',
                            width: '200', align: 'center',
                            formatter: function (value, row, index) {
                                var a = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="结算规则" onclick="acctTenantOperatorElecStation.acctRule(\''
                                    + row.operatorStationId
                                    + '\')">结算规则</a> ';

                                var b = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="电子合同" onclick="acctTenantOperatorElecStation.acctContract(\''
                                    + row.operatorStationId
                                    + '\')">电子合同</a> ';

                                var c = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="补充账单" onclick="acctTenantOperatorElecStation.saveAcctBillOrder(\''
                                    + row.elecStationId  +'\',\'' + row.tenantOperatorCode +'\',\'' + row.tenantCode
                                    + '\')">补充账单</a> ';

                                return a + b + c;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function acctRule(operatorStationId) {
        location.href = prefix + "/acctRuleList/" + operatorStationId;
    }

    function acctContract(operatorStationId) {
        location.href = prefix + "/acctContract/" + operatorStationId;
    }

    function exportAcctTenantOperatorElecStation() {
        $.fileDownload(prefix + "/exportAcctTenantOperatorElecStation", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                tenantOperatorCode: $("#tenantOperatorCode").val().trim(),
                stationName: $("#stationName").val().trim(),
                startTime: $("#startTime").val().trim(),
                endTime: $("#endTime").val().trim(),
                // count: $("#count").val().trim() == '' ? 0 : $("#count").val().trim(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }

    function saveAcctBillOrder(elecStationId,tenantOperatorCode,tenantCode){
        $("#exampleModal").modal("show");
        $("#addAcctBillOrder").unbind("click").bind("click",function () {
            var billStartTime=$("#billStartTime").val();
            var billEndTime=$("#billEndTime").val();
            var billTotalElecMoney=$("#billTotalElecMoney").val();
            var billTotalSeviceMoney=$("#billTotalSeviceMoney").val();
            var billTotalMoney=$("#billTotalMoney").val();
            var supplyRemark=$("#supplyRemark").val();
            if (billStartTime==''){
                alert("开始时间为空");
                return;
            }
            if (billEndTime==''){
                alert("结束时间为空");
                return;
            }
            if (billTotalElecMoney==''){
                alert("电费为空");
                return;
            }
            if (billTotalSeviceMoney==''){
                alert("服务费为空");
                return;
            }
            $.ajax({
                "type": "POST",
                "url": prefix + "/saveAcctBillOrder",
                "data": {
                    "elecStationId":elecStationId,
                    "tenantOperatorCode":tenantOperatorCode,
                    "tenantCode":tenantCode,
                    "billStartTime":billStartTime,
                    "billEndTime":billEndTime,
                    "billTotalElecMoney":billTotalElecMoney,
                    "billTotalSeviceMoney":billTotalSeviceMoney,
                    "billTotalMoney":billTotalMoney,
                    "supplyRemark":supplyRemark
                },
                "success": function (data) {
                    if(data.status==200){
                        parent.layer.alert("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        $("#acctBankId").val(null)
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                    }
                }
            })
        })

        $("#cancle").unbind("click").bind("click",function () {
            document.getElementById("contentForm").reset();
        })
    }

    return {
        init: init,
        reLoad: reLoad,
        acctRule: acctRule,
        acctContract: acctContract,
        exportAcctTenantOperatorElecStation: exportAcctTenantOperatorElecStation,
        saveAcctBillOrder: saveAcctBillOrder
    };
})();
